// 2025/9/14
// 加油站

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int left = 0, right = 0, n = gas.size();
        int count = 0;
        do
        {
            count += gas[right];
            count -= cost[right];
            right = (right + 1) % n;
            while(left < n && left != right - 1 && (count < 0 || gas[left] < cost[left]))
            {
                count -= gas[left];
                count += cost[left];
                left++;
            }
        }while(left < n && left != right);
        if(left >= n)
            return -1;
        else
            return left;
    }
};